#import <Foundation/Foundation.h>
#import <objc/runtime.h>

#import <CoreLocation/CoreLocation.h>

#import "framework/FWCommon.h"

#import "PILocation.h"


@implementation PILocation


@synthesize title, description, coordinates;


// ******************************************************************************************
#pragma mark -
#pragma mark Initialisation and object management

- (id)init
{
    if ((self = [super init])) {
        coordinates = nil;
        coordinatesSet = NO;
    }
    
    return self;
}

- (void)dealloc
{
    [title release];
    [description release];
    [coordinates release];
    
    [super dealloc];
}


+ (PILocation*)piLocationWithTitle:(NSString*)newTitle
{
    PILocation* loc = [[[PILocation alloc] init] autorelease];
    
    loc->title = [newTitle copy];
    loc->description = nil;
    loc->coordinatesSet = NO;
    
    return loc;
}

+ (PILocation*)piLocationWithTitle:(NSString*)newTitle description:(NSString*)newDescription coords:(CLLocation*)newCoords
{
    PILocation* loc = [[[PILocation alloc] init] autorelease];
    
    loc->title = [newTitle copy];
    loc->description = [newDescription copy];
    loc->coordinates = [newCoords copy];
    loc->coordinatesSet = YES;
    
    return loc;
}


// ******************************************************************************************
#pragma mark -
#pragma mark Information access

//- (void)setLocation:(NSString*)newLocation
//{
////    if (newLocation && (newLocation.length > 0))
////        newLocation = nil;
////    
////    [newLocation retain];
////    [location release];
////    location = newLocation;
//}

- (NSString*)title
{
    if (!coordinatesSet) {
        coordinatesSet = YES;
        
        // Remove any PI coordinates in the location information
        if (title) {
            if ([title rangeOfString:@"[" options:NSLiteralSearch].location != NSNotFound) {
                NSRange pos = [title rangeOfString:@"\\[-?[0-9]+\\.[0-9]+,-?[0-9]*\\.[0-9]+\\]"
                                           options:NSRegularExpressionSearch];
                if (pos.location != NSNotFound) {
                    NSString* coords = [[title substringWithRange:pos] substringFromIndex:1];
                    coords = [coords substringToIndex:coords.length - 1];
                    
                    NSString* newLocation = [[title substringToIndex:pos.location]
                                             stringByAppendingString:[title substringFromIndex:pos.location + pos.length]];
                    
                    NSArray* array = [coords componentsSeparatedByString:@","];
                    if (array.count == 2) {
                        NSNumber* latitude = [array objectAtIndex:0];
                        NSNumber* longitude = [array objectAtIndex:1];
                        
                        // Make coordinate from the passed values
                        coordinates = [[CLLocation alloc] initWithLatitude:latitude.doubleValue longitude:longitude.doubleValue];
                    }                    
                    
                    [title release];
                    title = [newLocation retain];
                }
            }
        }
    }
    
    return title;
}

- (void)setCoordinates:(CLLocation*)newCoords
{
    if (coordinatesSet)
        [coordinates release];

    coordinates = [newCoords retain];
    coordinatesSet = YES;
}

- (CLLocation*)coordinates
{
    if (!coordinatesSet)
        [self title];
    
    return coordinates;
}

@end
